package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.MessageDto;
import com.qf.commerce.common.vo.MessageVo;
import com.qf.commerce.entity.Channel;
import com.qf.commerce.entity.Message;
import com.qf.commerce.mapper.ChannelMapper;
import com.qf.commerce.mapper.MessageMapper;
import com.qf.commerce.service.MessageService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import javax.annotation.Resource;
import java.util.List;
/**
 * @author Administrator
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    MessageMapper messageMapper;
    @Resource
    ChannelMapper channelMapper;
    @Override
    public int addMessage(MessageDto messageDto) {
        Message message = CommonBeanUtils.copyForBean(messageDto, Message::new);
        Channel channel = channelMapper.selectChannelByName(message.getMessageType());
        if(ObjectUtils.isEmpty(channel)){
            throw new RuntimeException();
        }
        channel.setArticleCount(channel.getArticleCount()+1);
        int num1 = channelMapper.changeArticleCount(channel.getChannelName(), channel.getArticleCount());
        int num2 = messageMapper.insertMessage(message);
        return num1+num2;


    }

    @Override
    public int modifyMessage(MessageDto messageDto) {
        Message newMessage = CommonBeanUtils.copyForBean(messageDto, Message::new);
        Message oldMessage = messageMapper.selectMessageById(newMessage.getMessageId());
        Channel newChannel = channelMapper.selectChannelByName(newMessage.getMessageType());
        Channel oldChannel= channelMapper.selectChannelByName(oldMessage.getMessageType());
        newChannel.setArticleCount(newChannel.getArticleCount()+1);
        oldChannel.setArticleCount(oldChannel.getArticleCount()-1);
        int num1 = channelMapper.changeArticleCount(newChannel.getChannelName(), newChannel.getArticleCount());
        int num2 = channelMapper.changeArticleCount(oldChannel.getChannelName(), oldChannel.getArticleCount());
        int num3 = messageMapper.updateMessage(newMessage);
        return num1+num2+num3;

    }

    @Override
    public int deleteMessage(Long messageId) {
        return messageMapper.deleteMessageById(messageId);
    }

    @Override
    public int deleteMessages(List<Long> messageIds) {
        return messageMapper.deleteMessageByIds(messageIds);
    }

    @Override
    public int switchMessageStatus(MessageDto messageDto) {
        Message message = CommonBeanUtils.copyForBean(messageDto, Message::new);
        Message m = messageMapper.selectMessageById(message.getMessageId());
        if(ObjectUtils.isEmpty(m)){
            throw new RuntimeException();
        }
        if(m.getRecommendType()==1){
            return messageMapper.updateMessageToColumnRecommend(m.getMessageId());
        }
            return messageMapper.updateMessageToTopRecommend(m.getMessageId());

    }

    @Override
    public PageInfo<MessageVo> selectMessageList(Integer currentPage) {
        return CommonBeanUtils.copyPage(PageHelper.startPage(currentPage,10).doSelectPageInfo(()->messageMapper.selectMessageList())
                ,new PageInfo<MessageVo>(),MessageVo::new);
    }

    @Override
    public PageInfo<MessageVo> selectMessageByBeans(MessageDto messageDto,Integer currentPage) {
        Message message = CommonBeanUtils.copyForBean(messageDto, Message::new);
        return CommonBeanUtils.copyPage(PageHelper.startPage(currentPage,10).doSelectPageInfo(()->messageMapper.selectMessageListByBeans(message))
                ,new PageInfo<MessageVo>(),MessageVo::new);
    }
}
